# navigation_bar.py
from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
                             QToolButton, QSizePolicy, QSpacerItem)
from PyQt6.QtCore import (Qt, QPropertyAnimation, QEasingCurve, pyqtSignal, QAbstractAnimation)
from PyQt6.QtGui import QIcon, QFont


class NavigationBar(QWidget):
	module_changed = pyqtSignal(int)  # 模块切换信号
	
	def __init__(self):
		super().__init__()
		self.is_collapsed = False
		self.buttons = []
		self.current_index = 0
		self.init_ui()
	
	def init_ui(self):
		self.setMinimumWidth(200)
		self.layout = QVBoxLayout()
		self.layout.setContentsMargins(5, 10, 5, 10)
		self.layout.setSpacing(5)
		
		# 折叠按钮
		self.toggle_btn = QToolButton()
		self.toggle_btn.setObjectName("toggleButton")
		self.toggle_btn.setCheckable(False)
		self.toggle_btn.setFixedSize(40, 40)
		self.toggle_btn.setIcon(QIcon(":/icons/menu.svg"))
		self.toggle_btn.clicked.connect(self.toggle_animation)
		self.layout.addWidget(self.toggle_btn, 0, Qt.AlignmentFlag.AlignHCenter)
		
		# 模块容器
		self.module_container = QVBoxLayout()
		self.module_container.setSpacing(5)
		self.layout.addLayout(self.module_container)
		
		# 底部弹簧
		self.layout.addSpacerItem(QSpacerItem(20, 40,
		                                      QSizePolicy.Policy.Minimum,
		                                      QSizePolicy.Policy.Expanding))
		
		self.setLayout(self.layout)
		self.apply_style()
	
	def add_module(self, name, icon=None):
		"""添加导航模块"""
		btn = QToolButton()
		btn.setObjectName(f"navButton{len(self.buttons)}")
		btn.setCheckable(True)
		btn.setAutoExclusive(True)  # 自动单选模式
		btn.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
		btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
		btn.setFixedHeight(45)
		
		# 设置图标和文本
		if icon:
			if isinstance(icon, str) and icon.startswith(":/"):
				btn.setIcon(QIcon(icon))
			else:
				btn.setText(icon)  # 支持文本图标
		btn.setText(name)
		
		# 设置初始状态
		if len(self.buttons) == 0:
			btn.setChecked(True)
		
		# 连接信号
		btn.clicked.connect(lambda: self.on_module_clicked(len(self.buttons)))
		
		self.module_container.insertWidget(len(self.buttons), btn)
		self.buttons.append(btn)
	
	def on_module_clicked(self, index):
		"""处理模块点击事件"""
		if index == self.current_index:
			return
		self.current_index = index
		self.module_changed.emit(index)
	
	def toggle_animation(self):
		"""添加颜色过渡动画"""
		# 在原有动画基础上增加颜色过渡
		color_anim = QPropertyAnimation(self, b"styleSheet")
		color_anim.setDuration(300)
		
		if self.is_collapsed:
			start_color = "#2A2F3D"
			end_color = "#2A2F3D"
		else:
			start_color = "#3A4050"
			end_color = "#2A2F3D"
		
		color_anim.setStartValue(f"background: {start_color};")
		color_anim.setEndValue(f"background: {end_color};")
		color_anim.start(QAbstractAnimation.DeletionPolicy.DeleteWhenStopped)
	
	def update_button_states(self, collapsed):
		"""更新按钮显示状态"""
		for btn in self.buttons:
			if collapsed:
				btn.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)
				btn.setFixedWidth(40)
			else:
				btn.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)
				btn.setMinimumWidth(180)
	
	def apply_style(self):
		"""应用新的差异化样式"""
		self.setStyleSheet("""
	        /* 侧边栏背景（与主内容区明显区分） */
	        QWidget {
	            background: #2A2F3D;  /* 深蓝灰色 */
	            border-right: 1px solid #1E232F;
	        }

	        /* 导航按钮基础样式 */
	        QToolButton {
	            background: transparent;
	            color: #9FA8BA;       /* 浅灰蓝色 */
	            border-radius: 6px;
	            text-align: left;
	            padding: 0 20px;
	            font: 14px 'Microsoft YaHei';
	            min-width: 160px;
	        }

	        /* 悬停效果（保持与主内容区不同的色系） */
	        QToolButton:hover {
	            background: #343B4D;  /* 中灰蓝色 */
	            color: #E0E4EB;
	        }

	        /* 选中状态（使用暖色形成对比） */
	        QToolButton:checked {
	            background: #FF6B6B;  /* 珊瑚红色 */
	            color: white;
	            border-left: 3px solid #FF8787;
	        }

	        /* 折叠按钮样式 */
	        #toggleButton {
	            background: #3A4050;  /* 比侧栏稍浅 */
	            border-radius: 8px;
	            icon-size: 24px;
	        }
	        #toggleButton:hover {
	            background: #444B5D;
	        }

	        /* 图标颜色调整 */
	        QToolButton::icon {
	            color: #9FA8BA;
	            margin-left: 5px;
	        }
	        QToolButton:checked::icon {
	            color: white;
	        }
	        QWidget {
    box-shadow: 3px 0 15px rgba(0,0,0,0.1);
}

/* 按钮交互动画 */
QToolButton {
    transition: background 0.3s ease, color 0.3s ease;
}
	    """)
	